package com.springboot.blog.controller.admin;

import com.springboot.blog.constants.UploadConstants;
import com.springboot.blog.dto.LayResponse;
import com.springboot.blog.dto.Result;
import com.springboot.blog.entity.Pic;
import com.springboot.blog.service.IPicService;
import com.springboot.util.QiNiuYun;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Controller
class AdminUploadController {
    @Autowired
    IPicService picService;

    @RequiresPermissions("upload:img")
    @PostMapping("admin/upload/img")
    @ResponseBody
    public LayResponse img(@RequestParam(value = "file") MultipartFile file, @RequestParam(defaultValue = "other") String type) {
        String suffixName = UploadConstants.getSuffixName(file);
        //生成文件名称通用方法
        //创建文件
        String newFileName = type + "/" + UploadConstants.getNewFileName(suffixName);
        LayResponse response = new LayResponse();
        //判断是否上传七牛云
        if (UploadConstants.UPLOAD_QINIU) {
            //上传七牛云
            try {
                Result<Pic> result = QiNiuYun.uploadImg(file.getInputStream(), newFileName);
                if (result.succeed()) {
                    Pic pic = result.getData();
                    pic.setUrl(UploadConstants.HOST + pic.getUrl());
                    pic.setTitle(file.getOriginalFilename());
                    pic.setLastTime(LocalDateTime.now());
                    HashMap<String, String> map = new HashMap<>();
                    map.put("src", pic.getUrl());
                    response.success(file.getOriginalFilename() + "上传成功").data(map);
                    //保存照片到数据库
                    picService.save(pic);
                } else {
                    response.error("图片上传失败!" + result.getMessage());
                }
            } catch (IOException e) {
                e.printStackTrace();
                response.error("图片上传失败!" + e.getMessage());
            }
        } else {
            //上传本地
            File destFile = new File(UploadConstants.BASE_PATH + type);
            if (!destFile.exists()) {
                destFile.mkdirs();
            }
            try {
                file.transferTo(new File(UploadConstants.BASE_PATH + newFileName));
                HashMap<String, String> map = new HashMap<>();
                map.put("src",UploadConstants.ACCESS_PATH + newFileName);
                return response.success(file.getOriginalFilename() + "上传成功").data(map);
            } catch (IOException e) {
                response.error("图片上传失败!" + e.getMessage());
            }
        }
        return response;
    }

    /**
     * markdown图片上传接口
     *
     * @param file
     * @param type
     * @return
     */
    @RequiresPermissions("upload:img")
    @PostMapping("admin/upload/upload_img_markdown")
    @ResponseBody
    public Map upload_img_markdown(@RequestParam(value = "editormd-image-file") MultipartFile file, @RequestParam(defaultValue = "md") String type) {
        LayResponse response = img(file, type);
        HashMap<Object, Object> md = new HashMap<>();
        if (response.hasError()) {
            md.put("success", 0);
        } else {
            md.put("success", 1);
            Map data = (Map) (response.getData());
            md.put("url", data.get("src"));
        }
        md.put("message", response.getMsg());
        return md;
    }
}
